package com.mrd.gtimpframe.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.mrd.base.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimpframe.entity.Declaration;
import com.mrd.gtimpframe.entity.RejectRecord;
import com.mrd.gtimpframe.repository.DeclarationMapper;
import com.mrd.gtimpframe.service.DeclarationService;
import com.mrd.gtimpframe.service.RejectRecordService;
import com.mrd.util.JsonBinder;
import com.mrd.util.LoginUtil;
import com.mrd.util.ResponseUtils;

/**
 * Created by dusy on 2017/8/9.
 */
@Service
public class DeclarationServiceImpl implements DeclarationService {
    @Resource
    private DeclarationMapper declarationMapper;
    @Resource
    private RejectRecordService rejectRecordService;

    /**
     * 查询已驳回单证
     */
    @Override
    public void findRejectList(Jqgrid jqgrid, HttpServletResponse response) {
        // 添加分页和排序
        jqgrid.pageWithMybatis();
        jqgrid.sortWithMybatis();
        Map<String, String> propertyMap = new HashMap<>();
        // 添加查询条件
        propertyMap.put("whereCase", jqgrid.getWhereCase());
        // 添加需要获取的字段
        propertyMap.put("id", "");
        propertyMap.put("businessId", "");
        propertyMap.put("declarationType", "");
        propertyMap.put("sno", "");
        propertyMap.put("containerNo", "");
        propertyMap.put("blNo", "");
        propertyMap.put("contractNo", "");
        propertyMap.put("companyName", "");
        propertyMap.put("confirmTime", "");
        propertyMap.put("creator", "");
        propertyMap.put("enter_page", "");
        List<RejectRecord> rejectRecords = rejectRecordService.selectByWhereCase(propertyMap);
        PageInfo<RejectRecord> pi = new PageInfo<>(rejectRecords);
        JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
        jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        ResponseUtils.renderJson(response, jsonBinder.toJson(new PageImpl(rejectRecords, jqgrid.getPage(), pi.getTotal())));
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return declarationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Declaration record) {
        return declarationMapper.insert(record);
    }

    @Override
    public int insertSelective(Declaration record) {
        return declarationMapper.insertSelective(record);
    }

    @Override
    public Declaration selectByPrimaryKey(String id) {
        return declarationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Declaration> selectByPrimaryKeyList(List<String> idList) {
        return declarationMapper.selectByPrimaryKeyList(idList);
    }

    @Override
    public int updateByPrimaryKeySelective(Declaration record) {
        return declarationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Declaration record) {
        return declarationMapper.updateByPrimaryKey(record);
    }

    /**
     * 自定义查询条件及字段查询(只包含少部分查验信息)
     *
     * @param propertyMap 需要获取的报关单属性,whereCase:查询条件
     */
    @Override
    public List<Declaration> selectByWhereCaseIncludeCheckTracking(Map<String, String> propertyMap) {
        return declarationMapper.selectByWhereCaseIncludeCheckTracking(propertyMap);
    }

    /**
     * 待受理报关单查询
     *
     * @author han
     */
    public void findToValidList(Jqgrid jqgrid, HttpServletResponse response) {
        if (!LoginUtil.getLoginUser().getCompany().getName().equals("佛山市佛航展翔物流有限公司") || !LoginUtil.getLoginUser().getType().equals("0")) {
            String companyId = LoginUtil.getLoginUser().getCompany().getId();
            jqgrid.addRule(new SearchRule("T_BIZ_BASE_BUSINESS.SUP_ID", SearchRuleOption.eq, companyId));
        }
        jqgrid.addRule(new SearchRule("T_BIZ_DECLARATION.STATE", SearchRuleOption.eq, "DS01"));
        List<String> paymentTypes = new ArrayList<>();
        jqgrid.addRule(new SearchRule("T_BIZ_DECLARATION.PAYMENT_TYPE", SearchRuleOption.in, "1,2"));
        jqgrid.sortWithMybatis();
        jqgrid.pageWithMybatis();
        String whereCase = jqgrid.getWhereCase();
        Map<String, String> propertyMap = new HashMap<>();
        if (whereCase.contains("ACCEPTED_RESULT='appliedReject'")) {
            whereCase = whereCase.replace("ACCEPTED_RESULT='appliedReject' and", "");
            propertyMap.put("appliedReject", "");
        } else if (whereCase.contains("ACCEPTED_RESULT='notApplyReject'")) {
            whereCase = whereCase.replace("ACCEPTED_RESULT='notApplyReject' and", "");
            propertyMap.put("notApplyReject", "");
        }
        propertyMap.put("whereCase", whereCase);
        propertyMap.put("id", "");
        propertyMap.put("businessId", "");
        propertyMap.put("workNo", "");
        propertyMap.put("sno", "");
        propertyMap.put("dataType", "");
        propertyMap.put("containerNo", "");
        propertyMap.put("blNo", "");
        propertyMap.put("companyName", "");
        propertyMap.put("state", "");
        propertyMap.put("declarationType", "");
        propertyMap.put("contractNo", "");
        propertyMap.put("confirmTime", "");
        propertyMap.put("validateTime", "");
        propertyMap.put("validatorId", "");
        propertyMap.put("customerComId", "");
        propertyMap.put("transportNa", "");
        propertyMap.put("shipName", "");
        propertyMap.put("shipCode", "");
        propertyMap.put("qptyno", "");
        propertyMap.put("qpdjzt", "");
        propertyMap.put("qpscsj", "");
        propertyMap.put("qpdrsj", "");
        try {
            List<com.mrd.gtimpframe.entity.Declaration> declarationList = declarationMapper.findToValidList(propertyMap);
            PageInfo<com.mrd.gtimpframe.entity.Declaration> declarationPageInfo = new PageInfo<>(declarationList);
            JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
            jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
            ResponseUtils.renderJson(response, jsonBinder.toJson(new PageImpl(declarationList, jqgrid.getPage(), declarationPageInfo.getTotal())));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 已受理报关单查询
     */
    @Override
    public void findValidList(Jqgrid jqgrid, HttpServletResponse response) {
        if (!LoginUtil.getLoginUser().getCompany().getName().equals("佛山市佛航展翔物流有限公司") || !LoginUtil.getLoginUser().getType().equals("0")) {
            String companyId = LoginUtil.getLoginUser().getCompany().getId();
            jqgrid.addRule(new SearchRule("T_BIZ_BASE_BUSINESS.SUP_ID", SearchRuleOption.eq, companyId));
        }
        jqgrid.addRule(new SearchRule("T_BIZ_DECLARATION.STATE", SearchRuleOption.ni, "DS00,DS01,DS03,DS030"));
        jqgrid.sortWithMybatis();
        jqgrid.pageWithMybatis();
        String whereCase = jqgrid.getWhereCase();
        Map<String, String> propertyMap = new HashMap<>();
        if (whereCase.contains("ACCEPTED_RESULT='appliedReject'")) {
            whereCase = whereCase.replace("ACCEPTED_RESULT='appliedReject' and", "");
            propertyMap.put("appliedReject", "");
        } else if (whereCase.contains("ACCEPTED_RESULT='notApplyReject'")) {
            whereCase = whereCase.replace("ACCEPTED_RESULT='notApplyReject' and", "");
            propertyMap.put("notApplyReject", "");
        }
        propertyMap.put("whereCase", whereCase);
        propertyMap.put("id", "");
        propertyMap.put("businessId", "");
        propertyMap.put("workNo", "");
        propertyMap.put("sno", "");
        propertyMap.put("dataType", "");
        propertyMap.put("containerNo", "");
        propertyMap.put("blNo", "");
        propertyMap.put("companyName", "");
        propertyMap.put("state", "");
        propertyMap.put("declarationType", "");
        propertyMap.put("contractNo", "");
        propertyMap.put("confirmTime", "");
        propertyMap.put("validateTime", "");
        propertyMap.put("validatorId", "");
        propertyMap.put("customerComId", "");
        propertyMap.put("transportNa", "");
        propertyMap.put("shipName", "");
        propertyMap.put("shipCode", "");
        propertyMap.put("qptyno", "");
        propertyMap.put("qpdjzt", "");
        propertyMap.put("qpscsj", "");
        propertyMap.put("qpdrsj", "");
        propertyMap.put("confirmType", "");
        propertyMap.put("creator", "");
        List<com.mrd.gtimpframe.entity.Declaration> declarationList = declarationMapper.findValidList(propertyMap);
        PageInfo<com.mrd.gtimpframe.entity.Declaration> declarationPageInfo = new PageInfo<>(declarationList);
        JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
        jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        ResponseUtils.renderJson(response, jsonBinder.toJson(new PageImpl(declarationList, jqgrid.getPage(), declarationPageInfo.getTotal())));
    }

    /**
     * 根据主键查找报关单（包含单一窗口需要的所有内容）
     */
    @Override
    public Declaration selectByPrimaryKey4SingleWindow(String id) {
        return declarationMapper.selectByPrimaryKey4SingleWindow(id);
    }
}
